//监听部分url的请求之前修改ua
chrome.webRequest.onBeforeSendHeaders.addListener(function (details) {
        var headers = details.requestHeaders;
        for (var i = 0, l = headers.length; i < l; ++i) {
            if (headers[i].name == 'User-Agent') {
                break;
            }
        }
        if (i < headers.length) {
            headers[i].value = "android  ios iphone (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/519.37";
        }
        return {requestHeaders: headers};
    },
    {urls: ["*://www.oschina.net/action/api*"]},
    ["blocking", "requestHeaders"]
);


//alert信息到页面,做信息提醒用,比如提醒登录
function alertMessageToPage(content) {
    chrome.runtime.sendMessage(
        {cmd: content},
        function (response) {
            if (response) {
                console.log(response);
            }
        }
    );
}

//以app的形式发送动弹
function pubAppTweet(content) {
    var settings = {
        type: "GET",
        url: "http://www.oschina.net/action/apiv2/tweet?content=" + content,
        dataType: "json",
        error: function (XHR, textStatus, errorThrown) {
        },
        success: function (data, textStatus) {
            if (1 == data.code) {
                console.log("pub tweet success");
                alertMessageToPage("pub tweet success");
                window.close();
            } else {
                alertMessageToPage(data.message);
            }
        }
    };
    $.ajax(settings);
}

//检查cookie
document.addEventListener('DOMContentLoaded', function () {
    if (!chrome.cookies) {
        chrome.cookies = chrome.experimental.cookies;
    }
    chrome.cookies.getAll({
        url: "http://www.oschina.net",
        name: "oscid"
    }, function (cookies) {
        if (cookies && cookies.length > 0) {
            console.log("login success");
        } else {
            alertMessageToPage("please login");
        }
    });
});


//一键复制最新的动弹
function oneKeyCopyNewTweet() {
    $.ajax({
        type: "GET",
        url: "http://www.oschina.net/action/apiv2/tweets?type=1",
        dataType: "json",
        success: function (data) {
            if (1 == data.code) {
                var firstTweetContent = data.result.items[0].content;
                pubAppTweet(firstTweetContent);
            } else {
                alertMessageToPage(data.message);
            }
        }
    });
}


document.addEventListener('DOMContentLoaded', function () {
    var div = document.getElementById("oneKeyCopyNewTweet");
    div.addEventListener('click', oneKeyCopyNewTweet);
});


//去tuling123通过问题拿到内容
function findContentByTuling123(question) {
    var apiKey = "4c2667a1772b14c6a222cb351985a5c5";
    var apiUrl = "http://www.tuling123.com/openapi/api?key=" + apiKey + "&info=" + question;
    var answer = "";
    $.ajax({
        type: "GET",
        url: apiUrl,
        dataType: "json",
        async: false,
        success: function (data) {
            if (100000 == data.code) {
                answer = data.text;
            } else {
                alertMessageToPage("sorry,find content by tuling123 fail !");
            }
        }
    });
    return answer;
}


function oneKeyJokeTweet() {
    var content = findContentByTuling123("讲个笑话");
    if (content && content.length > 0) {
        pubAppTweet(content);
    } else {
        console.log("oneKeyJokeTweet content is blank.");
    }
}


document.addEventListener('DOMContentLoaded', function () {
    var div = document.getElementById("oneKeyJokeTweet");
    div.addEventListener('click', oneKeyJokeTweet);
});


function oneKeyWeatherTweet() {
    var content = findContentByTuling123("深圳天气");
    if (content && content.length > 0) {
        pubAppTweet(content);
    } else {
        console.log("oneKeyWeatherTweet content is blank.");
    }
}

document.addEventListener('DOMContentLoaded', function () {
    var div = document.getElementById("oneKeyWeatherTweet");
    div.addEventListener('click', oneKeyWeatherTweet);
});


var autoReportFlag = false;  //自动举报标志为false
//是否开启自动举报
function isOpenAutoReport() {
    var status = $("#openOrCloseAutoReport").data("status");
    return "off" != status;
}

//改变自动举报状态,关闭或者打开
function changeAutoReportStatus() {
    var openOrCloseAutoReportObject = $("#openOrCloseAutoReport");
    var status = openOrCloseAutoReportObject.data("status");
    if ("off" == status) {
        status = "on";
        openOrCloseAutoReportObject.data("status", status);
        openOrCloseAutoReportObject.css("color", "green");
        autoReportFlag = true;
        return status;
    } else {
        status = "off";
        openOrCloseAutoReportObject.data("status", status);
        openOrCloseAutoReportObject.css("color", "#000000");
        autoReportFlag = false;
        return status;
    }
}


document.addEventListener('DOMContentLoaded', function () {
    var div = document.getElementById("openOrCloseAutoReport");
    div.addEventListener('click', changeAutoReportStatus);
});












